const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    threeStatus:'none',
    searchDel:false,
    typecn:'所有分类',
    addrcn:'所在地区',    
    t:0,
    indext:0,
    timeList:[{name:'信息类型',code:0},{name:'供应',code:1},{name:'求购',code:2},{name:'终端求购',code:4}],   
    timeStatus:'none',
    typeStatus:'none',
    addrStatus:'none',
    fullStatus:'none',
    iconTime:' icon-xiangxia',
    iconType:' icon-xiangxia',
    iconAddr:' icon-xiangxia',
    zcList:[],
    GqClassList:[{GQ_IndexId:0,GQ_classid:0,GQ_classname:'所有分类',GQ_Lev:1}],
    GQ_IndexIdLev1:0,
    GQ_IndexLev1:0,
    GQ_classidLev1:0,
    GQ_Lev:0,  
    GqClassListLev1:[],
    GQ_IndexLev2:0,
    GQ_IndexIdLev2:0,
    GQ_classidLev2:0,  
    c:0,
    k:'',
    p:0,
    p1:0,
    p2:0,
    indexp1:0,
    indexp2:0,
    strPage:15,
    ProvinceAndCity:[{pcid: "0", code: "110000", name: "全国"}],
    TempTimeS:'',
    TempTimeX:'',
    hasMore:true,
    noMore:false,
    intCurrPage:1,
    ADList:[],
    perShow:10    
  },
  //选择了c1
  GqClassListLev1:function (e) {     
      this.setData({
        GQ_IndexIdLev1:e.currentTarget.dataset.gqindexid,
        GQ_IndexLev1:e.currentTarget.dataset.index,
        GQ_classidLev1:e.currentTarget.dataset.gqclassid, 
        GQ_Lev:e.currentTarget.dataset.gqlev  
      })
      if (e.currentTarget.dataset.gqclassid == 0) {
        this.setData({
          c:e.currentTarget.dataset.gqclassid,
          typecn:'所有分类'
        })
        this.changeType();
        this.getGqList()
      }else{
        var that = this
        var gqlev1 = [{GQ_IndexId:0,GQ_classid:0,GQ_classname:'不限',GQ_Lev:2}];
        this.getGqClass(this.data.GQ_IndexIdLev1,this.data.GQ_Lev,gqlev1,function (res) {        
            that.setData({
              GqClassListLev1:res
            })
        })        
      }
  },
  GqClassListLev2:function (e) {
    this.setData({
      GQ_IndexIdLev2:e.currentTarget.dataset.gqindexid,
      GQ_IndexLev2:e.currentTarget.dataset.index,
      GQ_classidLev2:e.currentTarget.dataset.gqclassid,
      // GQ_Lev:e.currentTarget.dataset.gqlev    
    })
    if (e.currentTarget.dataset.gqclassid == 0) {
      this.setData({
        c:this.data.GQ_classidLev1,
        typecn:this.data.GqClassList[this.data.GQ_IndexLev1].GQ_classname
      })
      this.changeType();
      this.getGqList()
    }else{
      this.setData({
        c:e.currentTarget.dataset.gqclassid,
        typecn:this.data.GqClassListLev1[e.currentTarget.dataset.index].GQ_classname
      })
      this.changeType();
      this.getGqList()   
      
      // var that = this
      //   var gqlev1 = [{GQ_IndexId:0,GQ_classid:0,GQ_classname:'不限',GQ_Lev:3}];
      //   this.getGqClass(this.data.GQ_IndexIdLev2,this.data.GQ_Lev,gqlev1,function (res) {        
      //       that.setData({
      //         GqClassListLev2:res
      //       })
      //   })     
    }

  },
  GqClassListLev3:function (e) {
    this.setData({
      GQ_IndexIdLev3:e.currentTarget.dataset.gqindexid,
      GQ_IndexLev3:e.currentTarget.dataset.index,
      GQ_classidLev3:e.currentTarget.dataset.gqclassid,
      GQ_Lev:e.currentTarget.dataset.gqlev    
    })
    if (e.currentTarget.dataset.gqclassid == 0) {
      this.setData({
        c:this.data.GQ_classidLev2,
        typecn:this.data.GqClassListLev1[this.data.GQ_IndexLev2].GQ_classname
      })
      this.changeType();
      this.getGqList()
    }else{
      this.setData({
        c:e.currentTarget.dataset.gqclassid,
        typecn:this.data.GqClassListLev2[e.currentTarget.dataset.index].GQ_classname
      })
      this.changeType();
      this.getGqList()   
      
      // var that = this
      //   var gqlev1 = [{GQ_IndexId:0,GQ_classid:0,GQ_classname:'不限',GQ_Lev:3}];
      //   this.getGqClass(this.data.GQ_IndexIdLev2,this.data.GQ_Lev,gqlev1,function (res) {        
      //       that.setData({
      //         GqClassListLev2:res
      //       })
      //   })     
    }

  },
  searchDelF:function () {
       this.setData({
        k:'',
        searchDel:false
      })
      this.getGqList()
  },
  zcczSearch:function (e) {    
    this.setData({
      k:app.trim(e.detail.value)
    }) 
    if (this.data.k.length > 0) {
      this.setData({
        searchDel:true
      })
    }else{
      this.setData({
        searchDel:false
      })      
    }  
  },
  searchBtn:function () {  
    this.getGqList()
  },
  //选择了地区省
  pSelect1:function (e) {
    this.setData({
      p1:e.currentTarget.dataset.po,
      indexp1:e.currentTarget.dataset.io
    })
    if (e.currentTarget.dataset.po == "00") {
      this.setData({
        p:0,
        addrcn:'所在地区'
      })
      this.changeAddr();
      this.getGqList()
    }   
  },
  pSelect2:function (e) {
    if (e.currentTarget.dataset) {
      if (e.currentTarget.dataset.pt == "0") {
        this.setData({
          addrcn:this.data.ProvinceAndCity[this.data.indexp1].name,
          p:this.data.p1
        })
        this.changeAddr();
        this.getGqList()
      }else{
        this.setData({
          p2:e.currentTarget.dataset.pt,
          indexp2:e.currentTarget.dataset.it,
          addrcn:this.data.ProvinceAndCity[this.data.indexp1].list[e.currentTarget.dataset.it].name,
          p:e.currentTarget.dataset.pt
        })
        this.changeAddr();
        this.getGqList()
      }
    }    

  },

  //选择了t
  tSelect:function (e) {
    if (e.currentTarget.dataset) {
      this.setData({
        t:e.currentTarget.dataset.t,
        indext:e.currentTarget.dataset.i
      })
      this.changeTime();
      this.getGqList()
    }
  },
  //点击选择时间
  changeTime:function () {
    if (this.data.timeStatus == 'none') {
       this.setData({
          typeStatus:'none',
          addrStatus:'none',          
          iconType:' icon-xiangxia',
          iconAddr:' icon-xiangxia',
          timeStatus:'block',
          fullStatus:'block',
          iconTime:' icon-xiangshang'
       })      
     }else{
       this.setData({
          timeStatus:'none',
          fullStatus:'none',
          iconTime:' icon-xiangxia'
       })      
     }
  },
  //点击选择地址
  changeAddr:function () {
    if (this.data.addrStatus == 'none') {
       this.setData({
          typeStatus:'none',
          timeStatus:'none',
          iconTime:' icon-xiangxia',
          iconType:' icon-xiangxia',         
          addrStatus:'block',
          fullStatus:'block',
          iconAddr:' icon-xiangshang'
       })      
     }else{
       this.setData({
          addrStatus:'none',
          fullStatus:'none',
          iconAddr:' icon-xiangxia'
       })      
     }
  },
  //点击选择类型
  changeType:function () {
    if (this.data.typeStatus == 'none') {
       this.setData({
          addrStatus:'none',
          timeStatus:'none',
          iconAddr:' icon-xiangxia',
          iconTime:' icon-xiangxia',
          typeStatus:'block',
          fullStatus:'block',
          iconType:' icon-xiangshang'
       })      
     }else{
       this.setData({
          typeStatus:'none',
          fullStatus:'none',
          iconType:' icon-xiangxia'
       })      
     }
  },  
  chickFull:function () {
    this.setData({
      timeStatus:'none',
      typeStatus:'none',
      addrStatus:'none',
      fullStatus:'none',
      iconTime:' icon-xiangxia',
      iconType:' icon-xiangxia',
      iconAddr:' icon-xiangxia'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      if (options.GQ_classid) {
        this.setData({
          c:options.GQ_classid,
          typecn:decodeURI(options.pageName)
        })
      }
      if (options.key) {
        this.setData({
          k:options.key,
          t:options.shid,
          indext:options.shid,
          searchDel:true
        })
      }
      if(options.shid){
        this.setData({
          t:options.shid,
          indext:options.shid,
          searchDel:true
        })
      }
      var that = this;
      var value = wx.getStorageSync('GqClassList');
      if (value) {
        this.setData({
          GqClassList:value
        })
      }else{
        wx.request({
          url: app.data.api_url+"api/FeijiuApp/GetGqClassList?GQ_IndexId=&GQ_classid=&GQ_Lev=&strRSA="+app.data.strRSA,        
          success: function(res) {
            if (res.statusCode == 200) {
              var zcList2 = res.data.data;
              var words = that.data.GqClassList.concat(zcList2);
              that.setData({
                GqClassList:words
              })             
              wx.setStorageSync('GqClassList',words)
            }
          }
        })        
      }

      for (var i = 0; i < this.data.GqClassList.length; i++) {
        if (this.data.GqClassList[i].GQ_classid == options.GQ_classid) {
          this.setData({
            GQ_IndexLev1:i,  
          })
        }
      }
      var gqlev1 = [{GQ_IndexId:0,GQ_classid:0,GQ_classname:'不限',GQ_Lev:2}];
      var gqindexid = options.GQ_classid+'.';
      this.getGqClass(gqindexid,1,gqlev1,function (res) { 
          that.setData({
            GqClassListLev1:res
          })
      })
      //存地区
      var ProAndCity = wx.getStorageSync('ProvinceAndCity');
      if (ProAndCity) {
        this.setData({
          ProvinceAndCity:ProAndCity
        })
      }else{
        wx.request({
          url: app.data.api_url+"api/FeijiuApp/GetCProvinceAndCity?strRSA="+app.data.strRSA,
          success: function(res) {
            if (res.statusCode == 200) {
              var addr1 = res.data.data
              var addr2 = [{pcid: "0", code: "110000", name: "城市不限"}]
              for (var i = 0; i < addr1.length; i++) {
                addr1[i].list.unshift(addr2[0])
              }
              var addr3 = that.data.ProvinceAndCity.concat(addr1);
              that.setData({
                ProvinceAndCity:addr3
              })
              wx.setStorageSync('ProvinceAndCity',addr3)
            }
          }
        })

      }

      this.Post_FJ_GetGQList_Page();
  },
  zcpic:function (arr) {
    var phonelist = [];
    if (arr) {
      phonelist = arr.split("|");
    }else{
      phonelist = [''];
    }    
    return phonelist[0];    
  },

  dateTimeChang:function (d) {      
      d = d.replace(/-/g, '/');
      return d 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      zcList:[],
      intCurrPage:1
    }) 
    this.Post_FJ_GetGQList_Page();
    wx.stopPullDownRefresh() 
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.noMore) {
      this.Post_FJ_GetGQList_Page();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  　　var that = this;  　　
  　　var shareObj = {
  　　　　title: that.data.zcMInfo.ZcInfo.Title,      
  　　　　path: '/pages/gongqiuxinxi/index?GQ_classid='+this.data.c+'&pageName='+this.data.typecn+'&key='+this.data.k+'&shid='+this.data.t,  
  　　　　imgUrl: '',
  　　}　
  　　return shareObj;    
  },
  //按条件加载
  loadZcList:function () {

  },

  Post_FJ_GetGQList_Page:function () {
    var that = this;
    var api_input = {
      cid:this.data.c,
      p:this.data.p,
      t:this.data.t,
      k:this.data.k,
      intCurrPage:this.data.intCurrPage,
      AppType:'小程序'
    }
    console.log(JSON.stringify(api_input))
    app.getDaoPost('api/FeijiuApp/Post_FJ_GetGQList_Page2020',api_input,function (r) {     
    console.log(r) 
      if (r.code == 200) {
      
        if (r.data.ds.ds.length > 0) {
          var vodauth = r.data.VideoAuth.split(",");

          var list1 = r.data.ds.ds;
          for (var i = 0; i < list1.length; i++) {
            list1[i].pic = that.zcpic(list1[i].photo);
            if (list1[i].ProAddress) {
              list1[i].ProAddress = list1[i].ProAddress.replace(/<\/?.+?>/g,"");
            } 
            
            if (vodauth.indexOf(list1[i].RankId) != -1 && list1[i].VideoPic.length > 6) {
              list1[i].is_vod = 1;
            }else{
              list1[i].is_vod = 0;
            }


          }
          var words = that.data.zcList.concat(list1);
          that.setData({
            zcList: words,
            ADList:r.data.ADList,
            intCurrPage:that.data.intCurrPage+1
          })
        }else{
          that.setData({
            hasMore: false,
            noMore: true
          })          
        }
      }
    })
  },

  //公共获取分类
  getGqClass: function(indexId, lev, defaultArr, cb) {
    wx.request({
      url: app.data.api_url + "api/FeijiuApp/GetGqClassList?GQ_IndexId=" + indexId + "&GQ_classid=&GQ_Lev=" + lev + "&strRSA=" + app.data.strRSA,
      success: function(res) {
        if (res.statusCode == 200) {
          var gqList2 = res.data.data;
          var words = defaultArr.concat(gqList2);
          return typeof cb == "function" && cb(words)
        }
      }
    })
  },
  preventTouchMove: function () {

  },
  getGqList:function() {
    this.setData({
      zcList:[],
      intCurrPage:1
    }) 
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 10
    })
    this.Post_FJ_GetGQList_Page();    
  },
  toshanpu:function (e) {

    var uid = e.currentTarget.dataset.uid;    
    var adclass = e.currentTarget.dataset.adclass;
    if(adclass == 6 || uid == 0){

    }else{
      wx.navigateTo({
        url: '../gongqiuxinxi/shangpu?longUID='+uid
      }) 
    }
  }

})